package com.stylefeng.guns.modular.outcome.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.stylefeng.guns.core.base.controller.BaseController;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;

import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;

import org.springframework.web.bind.annotation.RequestParam;

import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.persistence.model.Outcome;
import com.stylefeng.guns.modular.outcome.service.IOutcomeService;
import com.stylefeng.guns.modular.system.dao.OutcomeDao;

/**
 * 支出管理控制器
 *
 * @author fengshuonan
 * @Date 2021-01-05 19:42:08
 */
@Controller
@RequestMapping("/outcome")
public class OutcomeController extends BaseController {

    private String PREFIX = "/outcome/outcome/";

    @Autowired
    private IOutcomeService outcomeService;
    
    @Autowired
    private OutcomeDao outcomeDao;


    /**
     * 跳转到支出管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "outcome.html";
    }

    /**
     * 跳转到添加支出管理
     */
    @RequestMapping("/outcome_add")
    public String outcomeAdd(Model model) {
    	String userName = ShiroKit.getUser().getName();
    	model.addAttribute("userName",userName);
        return PREFIX + "outcome_add.html";
    }

    /**
     * 跳转到修改支出管理
     */
    @RequestMapping("/outcome_update/{outcomeId}")
    public String outcomeUpdate(@PathVariable Integer outcomeId, Model model) {
        Outcome outcome = outcomeService.selectById(outcomeId);
        model.addAttribute("item",outcome);
        LogObjectHolder.me().set(outcome);
        return PREFIX + "outcome_edit.html";
    }

    /**
     * 获取支出管理列表
     * @throws ParseException 
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required=false)Integer type) throws ParseException {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().defaultPage();
    	List<Map<String, Object>> lists = outcomeDao.selectByCondition(page, type);
    	List<Map<String, Object>> result = Lists.newArrayList();
    	for (Map<String, Object> map : lists) {
    		Map<String,Object> temp=Maps.newHashMap();
    		temp.put("id", map.get("id"));
    		temp.put("agent", map.get("agent"));
    		temp.put("explains", map.get("explains"));
    		temp.put("price", map.get("price"));
    		temp.put("number", map.get("number"));
    		temp.put("total", map.get("total"));
    		temp.put("createtime", map.get("createtime"));
    		temp.put("remark", map.get("remark"));
    		if(map.get("time")!=null){
    			temp.put("time", sdf.format(sdf.parse(map.get("time").toString())));
    		}else{
    			temp.put("time", "");
    		}
    		
    		if(map.get("type")!=null){
    			if(Integer.valueOf(map.get("type").toString())==1){
    				temp.put("type", "设备采购");
        		}
    			
    			if(Integer.valueOf(map.get("type").toString())==2){
    				temp.put("type", "活动经费");
        		}
    		}else{
    			temp.put("type", "");
    		}
    		
    		
    		result.add(temp);
		}
    	page.setRecords(result);
		System.out.println("总数"+page.getTotal());
        return super.packForBT(page);
    }

    /**
     * 新增支出管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Outcome outcome) {
    	outcome.setCreatetime(new Date());
        outcomeService.insert(outcome);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除支出管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long outcomeId) {
        outcomeService.deleteById(outcomeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改支出管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Outcome outcome) {
        outcomeService.updateById(outcome);
        return super.SUCCESS_TIP;
    }

    /**
     * 支出管理详情
     */
    @RequestMapping(value = "/detail/{outcomeId}")
    @ResponseBody
    public Object detail(@PathVariable("outcomeId") Integer outcomeId) {
        return outcomeService.selectById(outcomeId);
    }
}
